const nodemailer = require('nodemailer');  
  
// 定义多个QQ邮箱账户和参数  
const accounts = [  
    {  
        user: 'xuxiaowenfox@foxmail.com',  
        pass: 'pdgpsmhkpscscaaj',  
    },  
    {  
        user: '13128755602@qq.com',  
        pass: 'pdgpsmhkpscscaaj',  
    },  
    {  
        user: '531556397@qq.com',  
        pass: 'pdgpsmhkpscscaaj',  
    },
    {  
        user: 'aaa_5211@qq.com',  
        pass: 'pdgpsmhkpscscaaj',  
    },
    // ... 可以继续添加更多账户   aaa_5211@qq.com
];  
  
//定义要发送的数据
const subject="Hello from account"
var ADNB=0;
const text='This is a test email from the first account.'

// 定义要发送的邮件列表  
const emailsToSend = [  
    {  
        from: ADNB, // 使用第0个账户发送  
        to: '531556397@qq.com',  
        subject: subject+ ADNB,  
        text: text,  
    }

    
    // ,  
    // {  
    //     from: 1, // 使用第1个账户发送  
    //     to: '531556397@qq.com',  
    //     subject: subject+ (ADNB+1),  
    //     text: text,  
    // },  
    // ... 可以继续添加更多要发送的邮件  
];  

// 定义要发送的邮件列表  
const emailsToSend2 = [
    {
        from: ADNB, // 使用第0个账户发送  
        to: '531556397@qq.com',
        subject: subject & ADNB,
        text: text+"\n\n"+accounts[ADNB],
    }

];
  
// 发送邮件的函数  
async function sendEmail(accountIndex, emailData) {  
    try {  
        // 创建不同账号的邮件发送器  
        const transporter = nodemailer.createTransport({  
            service: 'qq',  
            auth: {  
                user: accounts[accountIndex].user,  
                pass: accounts[accountIndex].pass,  
            },  
        });  
        
        console.log( accounts[accountIndex])
  
        // 设置邮件内容  
        const mailOptions = {  
            from: accounts[accountIndex].user,  
            to: emailData.to,  
            subject: emailData.subject,  
            text: emailData.text,  
        };  
        console.log(emailData)
  
        // 发送邮件  
        const info = await transporter.sendMail(mailOptions);  
        console.log(`Message sent: ${info.messageId}`);  
    } catch (error) {  
        console.error(`Error sending email: ${error}`);  
    }  
}  
  
// 轮流使用不同账户发送邮件  
async function sendEmails() {  
    for (const Account of accounts) { 

        emailData=  {  
            from: ADNB, // 使用第0个账户发送  
            to: '531556397@qq.com',  
            subject: subject+ ADNB,  
            text: text,  
        }
        // const accountIndex = emailData.from;  
        const accountIndex = ADNB;  
        await sendEmail(accountIndex, emailData); 
        ADNB++;

    }  
}  
  
// 开始发送邮件  
sendEmails();